package com.studentmanagement.ui;

import com.studentmanagement.model.Enrollment;
import com.studentmanagement.service.EnrollmentService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class GradeEditDialog extends JDialog {
    private JLabel studentNameLabel;
    private JLabel courseNameLabel;
    private JTextField gradeField;
    private JComboBox<String> statusComboBox;
    private JButton saveButton;
    private JButton cancelButton;
    private boolean saved = false;
    private EnrollmentService enrollmentService = new EnrollmentService();
    private Enrollment enrollment;

    public GradeEditDialog(Enrollment enrollment) {
        this.enrollment = enrollment;
        setTitle("成绩录入");
        setModal(true);
        initComponents();
        setupLayout();
        setupListeners();
        fillForm(enrollment);
        pack();
        setLocationRelativeTo(null);
    }

    private void initComponents() {
        studentNameLabel = new JLabel();
        courseNameLabel = new JLabel();
        gradeField = new JTextField(10);
        String[] statusOptions = {"已选", "已退选", "已完成"};
        statusComboBox = new JComboBox<>(statusOptions);
        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");
    }

    private void setupLayout() {
        JPanel formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(6, 6, 6, 6);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        int row = 0;
        
        gbc.gridx = 0; gbc.gridy = row; formPanel.add(new JLabel("学生:"), gbc);
        gbc.gridx = 1; formPanel.add(studentNameLabel, gbc); row++;
        
        gbc.gridx = 0; gbc.gridy = row; formPanel.add(new JLabel("课程:"), gbc);
        gbc.gridx = 1; formPanel.add(courseNameLabel, gbc); row++;
        
        gbc.gridx = 0; gbc.gridy = row; formPanel.add(new JLabel("成绩:"), gbc);
        gbc.gridx = 1; formPanel.add(gradeField, gbc); row++;
        
        gbc.gridx = 0; gbc.gridy = row; formPanel.add(new JLabel("状态:"), gbc);
        gbc.gridx = 1; formPanel.add(statusComboBox, gbc); row++;

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        setLayout(new BorderLayout(10, 10));
        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void setupListeners() {
        saveButton.addActionListener(this::onSave);
        cancelButton.addActionListener(e -> dispose());
    }

    private void fillForm(Enrollment e) {
        studentNameLabel.setText(e.getStudentName() + " (" + e.getStudentNumber() + ")");
        courseNameLabel.setText(e.getCourseName() + " (" + e.getCourseCode() + ")");
        String grade = e.getGrade();
        if (grade != null && !grade.isEmpty()) {
            gradeField.setText(grade);
        }
        
        // 设置状态
        String status = e.getStatus();
        if ("ACTIVE".equals(status)) {
            statusComboBox.setSelectedItem("已选");
        } else if ("DROPPED".equals(status)) {
            statusComboBox.setSelectedItem("已退选");
        } else if ("COMPLETED".equals(status)) {
            statusComboBox.setSelectedItem("已完成");
        }
    }
    
    private String getStatusCode(String displayStatus) {
        switch (displayStatus) {
            case "已选": return "ACTIVE";
            case "已退选": return "DROPPED";
            case "已完成": return "COMPLETED";
            default: return "ACTIVE";
        }
    }

    private void onSave(ActionEvent e) {
        String gradeText = gradeField.getText().trim();
        
        // 只有当成绩不为空时才验证格式
        if (!gradeText.isEmpty()) {
            try {
                double grade = Double.parseDouble(gradeText);
                if (grade < 0 || grade > 100) {
                    JOptionPane.showMessageDialog(this, "成绩必须在0-100之间！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "成绩必须是有效的数字！", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
        }
        
        // 获取状态
        String selectedStatus = (String) statusComboBox.getSelectedItem();
        String statusCode = getStatusCode(selectedStatus);
        
        // 更新选课记录
        enrollment.setGrade(gradeText.isEmpty() ? null : gradeText);
        enrollment.setStatus(statusCode);
        
        boolean success = enrollmentService.updateEnrollment(enrollment);
        if (success) {
            saved = true;
            JOptionPane.showMessageDialog(this, "保存成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "保存失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public boolean isSaved() {
        return saved;
    }
} 